# Create your views here.
from django.shortcuts import render
from rest_framework.views import APIView,Response
from rest_framework.viewsets import ViewSet
from rest_framework.viewsets import ModelViewSet
from user.models import UserModel
from merchants.models import ShopsModel,GoodModel,GoodTypeModel
from merchants.serializer import ShopsSer,GoodModelSer,GoodTypeModelSer

from django.db.models import Q
from django.db import models
from utils.token_utils import check_token


# 注册商家
class Shops_register(APIView):
    def post(self,request):
        nickname = request.data.get('nickname')
        mobile = request.data.get('mobile')
        address = request.data.get('address')
        password = request.data.get("password")
        print('接收用户注册商家信息',nickname,mobile,address,password)

        if all([nickname,mobile,address]):
            try:
                ShopsModel.objects.get(nickname=nickname)
                return Response({'code': 204, 'msg': '商家名已被注册'})
            except Exception as e:
                print('查询商家错误',e)
            ShopsModel.objects.create(nickname=nickname,mobile=mobile,
                        address=address,password=password)
            return Response({'code': 200, 'msg': '注册商家成功'})
        return Response({'code': 204, 'msg': '注册商家信息不全'})


# 商家登录
class Shops_Login(APIView):
    def post(self,request):
        nickname = request.data.get('nickname')
        password = request.data.get('password')
        print('接收商家登录信息',nickname,password)

        user = ShopsModel.objects.get(nickname=nickname)
        if user.password == password:
            return Response({'code': 200, 'msg': '欢迎商家登录'})


# 商家信息管理
class ShopsViewSet(ViewSet):
    # 获取所有商家信息
    def get_shops_all(self,request):
        try:
            shops_set = ShopsModel.objects.all()
            print('获取所有商家信息', shops_set)
            shops_set = ShopsSer(shops_set,many=True)
            return Response({'code': 200, 'msg': '获取所有商家信息成功', 'data': shops_set.data})
        except Exception as e:
            print('获取所有商家信息失败',e)
            return Response({'code': 204, 'msg': '获取所有商家信息失败'})


    #获取一个商家信息
    def get_shops(self,request,pk):
        try:
            shops = ShopsModel.objects.get(user_id=pk)
            print('获取的商家信息', shops)
            shops_set = ShopsSer(shops)
            return Response({'code': 200, 'msg': '获取商家信息成功', 'shops_list': shops_set.data})
        except Exception as e:
            print('获取登录商家信息失败',e)
            return Response({'code': 204, 'msg': '获取商家信息失败'})


    @check_token    #修改商家信息
    def put_shops(self,request):
        print('sadas',request.user.id)
        shops_name = request.data.get('shops_name')
        shops_mobile = request.data.get('shops_mobile')
        shops_slogan = request.data.get('shops_slogan')
        shops_address = request.data.get('shops_address')
        open_time = request.data.get('open_time')
        end_time = request.data.get('end_time')
        sending_fee = request.data.get('sending_fee')
        id = request.user.id
        print('接受修改的数据',shops_name,shops_mobile)
        try:
            ShopsModel.objects.filter(user_id=id).update(shops_name=shops_name,
                shops_mobile=shops_mobile, shops_slogan=shops_slogan,
                shops_address=shops_address,sending_fee=sending_fee)
            return Response({'code':200,'msg':'修改商家信息成功'})
        except Exception as e:
            print('修改登录商家信息失败',e)
            return Response({'code':204,'msg':'修改商家信息失败'})

    @check_token    #注销商家
    def del_shops(self,request):
        id = request.user.id
        try:
            ShopsModel.objects.get(user_id=id).delete()
            return Response({'code':200,'msg':'注销商家成功'})
        except Exception as e:
            print('注销登录商家失败',e)
            return Response({'code':204,'msg':'注销商家失败'})


# 菜品种类
class KindViewSet(ViewSet):
    #获取所有菜品种类
    def get_kind_all(self,request,pk):
        print('接受的商家id',pk)
        try:
            shops = ShopsModel.objects.get(user_id=pk)
            print('这是获取的商家',shops)
        except Exception as e:
            print('获取商家失败', e)
            return Response({'code': 204, 'msg': '这个商家不存在'})

        try:
            shops_kind_set = GoodTypeModel.objects.filter(shops_id=shops.id)
            print('获取的商家所有菜品种类', shops_kind_set)
            kind_all = GoodTypeModelSer(shops_kind_set, many=True)
            return Response({'code': 200, 'msg': '获取商家所有菜品种类成功', 'kind_all': kind_all.data})
        except Exception as e:
            print('获取商家所有菜品种类失败', e)
            return Response({'code': 204, 'msg': '获取商家所有菜品种类失败'})


    @check_token    #添加种类
    def add_kind(self,request):
        kind_name = request.data.get('kind_name')
        id = request.user.id
        print('添加种类的数据',kind_name,id)
        try:
            shops = ShopsModel.objects.get(user_id=id)
            print('这是获取的商家',shops)
            try:
                GoodTypeModel.objects.create(kind_name=kind_name,shops_id=shops.id)
                return Response({'code':200,'msg':'商家添加菜品种类成功'})
            except Exception as e:
                print('商家添加菜品种类失败',e)
                return Response({'code':204,'msg':'商家添加菜品种类失败'})
        except Exception as e:
            print('获取登录商家失败', e)
            return Response({'code': 204, 'msg': '你不是商家,请注册'})


    @check_token    #修改种类信息
    def put_kind(self,request,pk):
        kind_name = request.data.get('kind_name')
        print('这是要修改的种类信息',pk,kind_name)
        try:
            shops = ShopsModel.objects.get(user_id=request.user.id)
        except Exception as e:
            print('获取商家失败', e)
            return Response({'code': 204, 'msg': '您不是商家'})
        try:
            GoodTypeModel.objects.filter(id=pk,shops_id=shops.id).update(kind_name=kind_name)
            return Response({'code': 200, 'msg': '商家修改菜品种类成功',})
        except Exception as e:
            print('商家修改菜品种类失败', e)
            return Response({'code': 204, 'msg': '商家修改菜品种类失败'})


    @check_token    #删除种类
    def del_kind(self,request,pk):
        try:
            shops = ShopsModel.objects.get(user_id=request.user.id)
            try:
                GoodTypeModel.objects.get(Q(id=pk) & Q(shops_id=shops.id))
            except Exception as e:
                print('商家删除种类失败',e)
                return Response({'code':204,'msg':'该商家无此菜品种类,删除失败'})
            GoodTypeModel.objects.filter(id=pk,shops_id=shops.id).delete()
            return Response({'code': 200, 'msg': '商家删除种类成功'})
        except Exception as e:
            print('获取商家失败', e)
            return Response({'code': 204, 'msg': '您不是商家,商家不存在'})


# 菜品信息管理
class DishesViewSet(ViewSet):
    #获取商家一个种类的所有菜品
    def get_kind_dishes_all(self,request,pk):
        shops_id = request.data.get('shops_id')
        print('获取的商家id',shops_id)
        try:
            shops = ShopsModel.objects.get(id=shops_id)
            print('这是获取的商家',shops)
        except Exception as e:
            print('获取登录商家失败', e)
            return Response({'code': 204, 'msg':'该商家不存在'})
        try:
            kind_dishes_set = GoodModel.objects.filter(Q(shops_id=shops_id) & Q(kind_id=pk))
            print('商家一个种类所有菜品', kind_dishes_set)
            dishes = GoodModelSer(kind_dishes_set,many=True)
            return Response({'code': 200, 'msg': '获取商家一个种类所有菜品成功', 'kind_dishes':dishes.data})
        except Exception as e:
            print('获取商家种类菜品所有菜品失败', e)
            return Response({'code': 204, 'msg': '获取商家种类菜品所有菜品失败'})

    #获取商家所有菜品
    def get_dishes_all(self,request,pk):
        try:
            shops = ShopsModel.objects.get(user_id=pk)
            print('这是获取的商家',shops)
        except Exception as e:
            print('获取登录商家失败', e)
            return Response({'code': 204, 'msg':'该商家不存在'})
        try:
            dishes_set = GoodModel.objects.filter(shops_id=shops.id)
            print('商家所有菜品', dishes_set)
            dishes = GoodModelSer(dishes_set,many=True)
            return Response({'code': 200, 'msg': '获取商家菜品所有菜品成功', 'dishes_all':dishes.data})
        except Exception as e:
            print('获取商家菜品所有菜品失败', e)
            return Response({'code': 204, 'msg': '获取商家菜品所有菜品失败'})

    # 获取一个菜品详情
    def get_dishes(self,request,pk):
        shops_id = request.data.get('shops_id')
        print('获取的商家id',shops_id)
        try:
            dishes = GoodModel.objects.get(models.Q(id=pk) & models.Q(shops_id=shops_id))
            dishes_ser = GoodModelSer(dishes)
            return Response({'code': 200, 'msg': '获取一个菜品成功', 'data':dishes_ser.data})
        except Exception as e:
            print('获获一个菜品失败', e)
            return Response({'code': 204, 'msg': '该商家无此菜品'})

    @check_token    #添加菜品
    def add_dishes(self,request):
        dishes_name = request.data.get('dishes_name')
        dishes_price = request.data.get('dishes_price')
        dishes_picture = request.data.get('dishes_picture')
        kind_id = request.data.get('kind_id')
        # id = request.user.id
        print('添加菜品的数据',dishes_name,dishes_price,dishes_picture,kind_id)
        try:
            shops = ShopsModel.objects.get(user_id=request.user.id)
        except Exception as e:
            print('获取登录商家失败', e)
            return Response({'code': 204, 'msg': '您还不是商家,无法添加菜品'})
        try:
            dishes_name = GoodModel.objects.get(dishes_name=dishes_name)
            return Response({'code': 204, 'msg': '该菜品已存在,无法添加'})
        except Exception as e:
            print('商家添加菜品失败',e)
            try:
                GoodModel.objects.create(dishes_name=dishes_name,dishes_price=dishes_price,
                     kind_id=kind_id,shops_id=shops.id)
                return Response({'code': 200, 'msg': '商家添加菜品成功'})
            except Exception as e:
                print('商家添加菜品失败',e)
                return Response({'code':204,'msg':'商家添加菜品失败'})

    def put_dishes(self,request,pk):
        dishes_name = request.data.get('dishes_name')
        dishes_price = request.data.get('dishes_price')
        dishes_picture = request.data.get('dishes_picture')
        kind_id = request.data.get('kind_id')
        print('修改菜品的数据',dishes_name,id)
        try:
            ShopsModel.objects.get(user_id=request.user.id)
        except Exception as e:
            print('获取登录商家失败', e)
            return Response({'code': 204, 'msg': '您不是商家,无法修改'})
        try:
            GoodModel.objects.filter(id=pk).update(dishes_name=dishes_name,
                dishes_price=dishes_price,kind_id=kind_id)
            return Response({'code': 200, 'msg': '修改菜品信息成功'})
        except Exception as e:
            print('商家修改菜品失败',e)
            return Response({'code':204,'msg':'商家修改菜品失败'})

    def del_dishes(self,request,pk):
        user_id = request.data.get("goods_user_id")
        try:
            shops = ShopsModel.objects.get(user_id=user_id)
        except Exception as e:
            print('获取登录商家失败', e)
            return Response({'code': 204, 'msg': '您不是商家'})

        try:
            GoodModel.objects.get(Q(id=pk) & Q(shops_id=shops.id))
            GoodModel.objects.get(id=pk).delete()
            return Response({'code': 200, 'msg': '商家删除菜品成功'})
        except Exception as e:
            print('获取菜品失败', e)
            return Response({'code': 204, 'msg': '您没有这道菜菜品'})


class Type(APIView):
    def get(self, request):
        types = GoodTypeModel.objects.all()
        types_ser = GoodTypeModelSer(types, many=Type)
        return Response({"code": 200, "msg": '成功', "data": types_ser.data})


class Good(APIView):
    def get(self, request):
        goods = GoodModel.objects.all()
        goods_ser = GoodModelSer(goods, many=Type)
        return Response({"code": 200, "msg":"获取成功", "data": goods_ser.data})


class TypeGood(APIView):
    def get(self, request, pk):
        if int(pk) == 0:
            goods = GoodModel.objects.all()
            goods_ser = GoodModelSer(goods,many=Type)
            return Response({"code": 200,"msg": "获取成功","data": goods_ser.data})
        else:
            goods = GoodTypeModel.objects.get(id=pk)
            goods = goods.goodmodel_set.all()
            goods_ser = GoodModelSer(goods, many=Type)
            return Response({"code": 200,"msg": "获取成功","data": goods_ser.data})

